﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyStep.Business.Interfaces;
using EasyStep.Data.Entities;

namespace EasyStep.Website.Controllers
{
    [Authorize]
    public class TestProjectController : Controller
    {
        private ITestProjectManager Projects { get; set; }
        private ITestUserManager Users { get; set; }
        private ITestConstantsListManager ConstsList { get; set; }
        private ITestConstantManager Consts { get; set; }
        private ITestInfoManager Tests { get; set; }
        public TestProjectController(ITestConstantManager tConst, ITestConstantsListManager tConstList, ITestProjectManager tProjects, ITestUserManager tUsers, ITestInfoManager tTests)
        {
            this.Consts = tConst;
            this.Projects = tProjects;
            this.Users = tUsers;
            this.ConstsList = tConstList;
            this.Tests = tTests;
        }
        /// <summary>
        /// show list of projects, their tests & constant list
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var model=Projects.GetUserProjects(Users.Current().Id).ToList();
            model.ForEach(p =>
            {
                p.Tests = Tests.GetProjectTests(p.Id);
                p.ConstantLists = ConstsList.GetProjectConstList(p.Id);
            });
            return View(model);
        }
       
        /// <summary>
        /// Provide a way to overview and quick edit list of project tests, constants
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(Guid id)
        {
            var model = Projects.GetById(id, ConstsList, Tests);
            if (!Users.Authorize(model))
                RedirectToAction("Authorize", "Account");
            return View(model);
        }

        //
        // GET: /TestProject/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TestProject/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TestProject/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /TestProject/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TestProject/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /TestProject/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpGet]
        public JsonResult AvailableProjects(string term)
        {
             var projects=GetAvailableProjects();
            if (!string.IsNullOrEmpty(term))
                projects=projects.Where(p=>p.Name.StartsWith(term,StringComparison.OrdinalIgnoreCase));
            return Json(projects, JsonRequestBehavior.AllowGet);
        }
       

        private IEnumerable<TestProject> GetAvailableProjects()
        {
            var projects = Projects.GetUserProjects(Users.Current().Id).ToList();
            return projects;
        }
    }
}
